package model

import (
	"strconv"
	"strings"
	"time"
)

type Column struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	QuestionItemList []*QuestionItem `xorm:"-"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

type QuestionItem struct {
	Id int64 `xorm:"pk autoincr"`
	CustomerId int64
	ColumnId int64
	Value int
	Name string
	HistoricalData string
	CustomerFillData string
	SystemCurrentData string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Answer struct {
	CustomerId int64
	Age string
	Height string
	TargetWeight string
	CurrentWeight string
	Bmi string
	FollowingProblem string
	FollowingSymptom string
	FamilyHistory string
	TriedWeight string
	Defecation string
	DietPreference string
	SleepTime string
	SleepQuality string
	Sport string
	EnteredBy string
	EntryTime string
}

type Question struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	Value int
	SpecificValue int
	Select bool //是否选择题
	OptionList []*Option `xorm:"-"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Option struct {
	Id int64 `xorm:"pk autoincr"`
	QuestionId int64
	Name string
	BeElected bool `xorm:"-"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (answer *Answer)FillQuestionnaire(a *Answer)(map[string]interface{}, error){

	var qtLlist []*QuestionItem
	x.Where("customer_id = ?", a.CustomerId).Find(&qtLlist)

	for _, qt := range qtLlist{
		if qt.Name == "年龄"{
			qt.CustomerFillData = a.Age
		}else if qt.Name == "身高"{
			qt.CustomerFillData = a.Height
		}else if qt.Name == "当前体重"{
			qt.CustomerFillData = a.CurrentWeight
		}else if qt.Name == "目标体重"{
			qt.CustomerFillData = a.TargetWeight
		}else if qt.Name == "BMI"{
			qt.CustomerFillData = a.Bmi
		}else if qt.Name == "身体是否有以下问题"{
			qt.CustomerFillData = a.FollowingProblem
		}else if qt.Name == "过去是否有以下问题"{
			qt.CustomerFillData = a.FollowingSymptom
		}else if qt.Name == "家族史"{
			qt.CustomerFillData = a.FamilyHistory
		}else if qt.Name == "尝试过的减肥方法"{
			qt.CustomerFillData = a.TriedWeight
		}else if qt.Name == "排便情况"{
			qt.CustomerFillData = a.Defecation
		}else if qt.Name == "饮食偏好"{
			qt.CustomerFillData = a.DietPreference
		}else if qt.Name == "睡眠时间"{
			qt.CustomerFillData = a.SleepTime
		}else if qt.Name == "睡眠质量"{
			qt.CustomerFillData = a.SleepQuality
		}else if qt.Name == "运动情况"{
			qt.CustomerFillData = a.Sport
		}else if qt.Name == "录入人"{
			qt.CustomerFillData = a.EnteredBy
		}else if qt.Name == "录入时间"{
			qt.CustomerFillData = a.EntryTime
		}

		x.Id(qt.Id).Update(qt)
	}

	return map[string]interface{}{"message":"成功","code":200}, nil
}

func (answer *Answer)Query(cId int64)([]*Column, error){

	var cList []*Column
	x.Find(&cList)
	for _, c := range cList{
		x.Where("customer_id = ?", cId).And("column_id = ?", c.Id).Find(&c.QuestionItemList)
	}
	return cList, nil
}

func (answer *Answer)ExternalCallQuery(cId int64)([]*Question, error){

	var qList []*Question
	x.Find(&qList)

	for _, q := range qList{

		var qi QuestionItem
		x.Where("customer_id = ?", cId).And("value = ?", q.Value).Get(&qi)

		if q.Select{
			x.Where("question_id = ?", q.Id).Find(&q.OptionList)
			for _, o := range q.OptionList{
				if qi.HistoricalData != ""{
					for _, e:= range strings.Split(qi.HistoricalData, "/"){
						if e == o.Name{
							o.BeElected = true
						}
					}
				}
			}
		}else {
			if qi.HistoricalData != ""{
				sv, err	:= strconv.Atoi(qi.HistoricalData)
				if err != nil{
					return nil, err
				}
				q.SpecificValue = sv
			}
		}
	}

	return qList, nil
}